<?php
/**
 * User: Titan
 * Date: Sep 29, 2010
 * Time: 11:12:01 PM
 */

$main = 'uninstall';

define('CONFIGURATION_MODE', 'UNINSTALL');

require_once ('./includes/uninstall.inc');

/**
 * uninstall()
 * 
 * The main uninstallation function
 * 
 * @return
 */
function uninstall()
{
	system_add_styles('assets/css/style.css');
}

/**
 * uninstall_output()
 * 
 * @param mixed $data
 * @return
 */
function uninstall_output($data = NULL)
{
	static $output = '';
	
	if (isset($data))
	{
		$output = $data;
	}
	
	return $output;
}

/*
 * returns the database configuration form
 */
function uninstall_confirmation_form($reload = FALSE)
{
	$form = array(
		'#id' => unique_id('uninstall_confirmation_form'), '#action' => 'uninstall.php?op=confirm', '#method' => 'POST', '#inputs' => array()
	);
	
	$form['#inputs']['confirm'] = array(
		'#title' => 'Are you sure you want to uninstall BugZapp?', '#type' => 'checkbox', '#description' => 'Your BugZapp database will be completely removed, if you choose to proceed. Making a backup is highly recommended.', '#value' => 1, '#checked' => FALSE
	);
	
	if ($reload === TRUE)
	{
		$form['#inputs']['confirm'] += array(
			'#prefix' => '<div style="color:red; font-style:italic;">', '#suffix' => '</div>'
		);
	}
	
	$form['#inputs']['submit'] = array(
		'#type' => 'submit', '#value' => 'Uninstall'
	);
	
	$output = form_build($form, 'uninstall_confirmation_form');
	
	return $output;
}

function uninstall_confirmation_form_validate(&$form_result)
{
	if (!isset($form_result['values']['confirm']) || $form_result['values']['confirm'] != 1)
	{
		form_error('confirm', 'Please confirm that you would like to uninstall the BugZapp database by checking the box below.');
	}
}

/**
 * uninstall_confirmation_form_submit()
 * 
 * @param mixed $form_values
 * @return
 */
function uninstall_confirmation_form_submit(&$form_result)
{
	uninstall_system();
	
	system_page_title('Uninstall Complete');
	uninstall_output('<p style="font-weight: bold;">BugZapp has been uninstalled. <a href="' . $GLOBALS['base_url'] . '">Click here</a> to return to the home page and reinstall BugZapp.</p>');
}

function uninstall_prerender_page(&$vars)
{
	$output = uninstall_output();
	
	if ($output == '')
	{
		$vars['title'] = 'Uninstall BugZapp';
		$output = uninstall_confirmation_form(form_get_errors());
		
		uninstall_output($output);
	}
	
	$vars['content'] = $output;
	$vars['tabs'] = NULL;
}
